package Crypto.xor;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * btoS
 * @author SNSCat2012
 *
 * This class converts between an ASCII representation of the integer byte 
 * values and the corresponding byte array.
 */
public class btoS {
	/**
	 * convertByteArrayToString
	 * @param in
	 * @return
	 * 
	 * Converts a byte array to a space separated integer byte value string.
	 */
	public static String convertByteArrayToString(byte [] in) {
		String out = "";
		for(int i = 0; i < in.length; i++) {
			out += Byte.valueOf(in[i]) + " ";
		}
		out.trim();
		
		return out;
	}
	
	/**
	 * convertStringToByteArray
	 * @param in
	 * @return
	 * @throws IOException
	 * 
	 * Converts a space separated string of integer byte values to the byte 
	 * representation.
	 */
	public static byte[] convertStringToByteArray(String in) 
			throws IOException {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		for(int i = 0; i < in.split(" ").length; i++) {
			out.write(Byte.parseByte(in.split(" ")[i]));
		}

		out.flush();
		
		return out.toByteArray();
	}
}
